//左边盒子交互
function getLeft(){
	$.ajax({
		type:'get',
		url:'/citrusmonitor/announce.do?method=showAnnounce&pageSize=8',
		dataType:'json',
		success:function(data){//传5~10条数据
//			console.log(data);
			var news_ul = document.getElementsByClassName("news_ul")[0];
			var html = '';
			$.each(data,function(i,n){
				html += '<li><a href="message.html?ci=99&id='+n["announcementId"]+'">'+n["title"]+'</a></li>';
			});
			news_ul.innerHTML = html;
		},
		error:function(){
			alert("资源加载失败！！");
		}
	});
	
}
getLeft();


//右边盒子交互
var curPage=1;//当前页面
var countPage;//后台告知总页面数
//请求后台共有几条数据
function countPage_fun(){
	$.ajax({
		type:'GET',
		url:'/citrusmonitor/question.do?method=showQCount&status=1',
		dataType:'JSON',
		success:function(data){
//			console.log(data);
			countPage=Math.ceil(data/10);
			showMessage(1,10);
		},
		error:function(){
			alert("页数加载失败！！");
		}
	});
}
countPage_fun();
//请求数据
function showMessage(page,count){
	//page 当前页数
	//count 请求数据数量
	var messUrl = '/citrusmonitor/question.do?method=showQ&status=1'+'&currentPage='+page+'&pageSize='+count; //后台填充URL
	$.ajax({
		type:'GET',
		url:messUrl,
		dataType:'JSON',
		success:function(data){
//			console.log(data);
			var list = document.getElementsByClassName("list")[0];
			var html = '';
			$.each(data,function(i,n){
				html += '<li><a href="show_ans_ques.html?questionId='+n["questionId"]+'" class="link"><span>'+n["summary"]+'</span><span>'+n["createTime"]+'</span></a></li>';
			});
			list.innerHTML = html;
			showBtn();
		},
		error:function(){
			alert("资源加载失败！！");
		}
	});
	
}
//显示页面数按钮
function showBtn(){
	var btn_list = document.getElementsByClassName("btn_list")[0];
	var btnNum = curPage;//按钮数字
	var html = '';
	var count = 5;//需要显示的按钮数
	if(curPage+4>countPage){
		count = countPage-curPage+1;
	}
	for(let i =0;i<count;i++){
		if(i==0){
			html += '<li class="active">'+ (btnNum++) +'</li>';
		}else{		
			html += '<li>'+ (btnNum++) +'</li>';
		}
	}

	btn_list.innerHTML = html;
	btnFun();

}

var pre = document.getElementsByClassName("pre")[0];
var next = document.getElementsByClassName("next")[0];

pre.onclick = function(){
	if(curPage==1){
		alert("当前已经是第一页");
	}else{
		curPage--;
//		alert("请求第"+curPage+"页数据");
		showMessage(curPage,10);
		showBtn();
	}

}

next.onclick = function(){
	if(curPage==countPage){
		alert("当前已经是最后一页");
	}else{
		curPage++;
//		alert("请求第"+curPage+"页数据");
		showMessage(curPage,10);
		showBtn();
	}
}	

//页面按钮功能
function btnFun(){
	
	var btn_list = document.getElementsByClassName("btn_list")[0];
	var list_li = btn_list.getElementsByTagName("li");
	for(let i=0,len=list_li.length;i<len;i++){
		list_li[i].onclick = function(){
			var clickPage = parseInt(list_li[i].innerHTML);
			for(let j=0,len=list_li.length;j<len;j++){
				if(list_li[j].classList.contains("active")){
					list_li[j].classList.remove("active");
					break;
				}
			}
			list_li[i].classList.add("active");
//			alert("请求第"+clickPage+"页数据");
			curPage = clickPage;
			showMessage(clickPage,10);
			showBtn();
		}
	}
}
